<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>xhrio_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_net_xhrio_test.js.html">xhrio_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.net.XhrIoTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.net.XhrIoTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.Uri&#39;);
<a name="line19"></a>goog.require(&#39;goog.debug.EntryPointMonitor&#39;);
<a name="line20"></a>goog.require(&#39;goog.debug.ErrorHandler&#39;);
<a name="line21"></a>goog.require(&#39;goog.debug.entryPointRegistry&#39;);
<a name="line22"></a>goog.require(&#39;goog.events&#39;);
<a name="line23"></a>goog.require(&#39;goog.functions&#39;);
<a name="line24"></a>goog.require(&#39;goog.net.EventType&#39;);
<a name="line25"></a>goog.require(&#39;goog.net.WrapperXmlHttpFactory&#39;);
<a name="line26"></a>goog.require(&#39;goog.net.XhrIo&#39;);
<a name="line27"></a>goog.require(&#39;goog.net.XmlHttp&#39;);
<a name="line28"></a>goog.require(&#39;goog.object&#39;);
<a name="line29"></a>goog.require(&#39;goog.string&#39;);
<a name="line30"></a>goog.require(&#39;goog.testing.MockClock&#39;);
<a name="line31"></a>goog.require(&#39;goog.testing.PropertyReplacer&#39;);
<a name="line32"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line33"></a>goog.require(&#39;goog.testing.net.XhrIo&#39;);
<a name="line34"></a>goog.require(&#39;goog.testing.recordFunction&#39;);
<a name="line35"></a>
<a name="line36"></a>function MockXmlHttp() {
<a name="line37"></a>  /**
<a name="line38"></a>   * The headers for this XmlHttpRequest.
<a name="line39"></a>   * @type {!Object.&lt;string&gt;}
<a name="line40"></a>   */
<a name="line41"></a>  this.headers = {};
<a name="line42"></a>}
<a name="line43"></a>
<a name="line44"></a>MockXmlHttp.prototype.readyState = goog.net.XmlHttp.ReadyState.UNINITIALIZED;
<a name="line45"></a>
<a name="line46"></a>MockXmlHttp.prototype.status = 200;
<a name="line47"></a>
<a name="line48"></a>MockXmlHttp.syncSend = false;
<a name="line49"></a>
<a name="line50"></a>MockXmlHttp.prototype.send = function(opt_data) {
<a name="line51"></a>  this.readyState = goog.net.XmlHttp.ReadyState.UNINITIALIZED;
<a name="line52"></a>
<a name="line53"></a>  if (MockXmlHttp.syncSend) {
<a name="line54"></a>    this.complete();
<a name="line55"></a>  }
<a name="line56"></a>
<a name="line57"></a>};
<a name="line58"></a>
<a name="line59"></a>MockXmlHttp.prototype.complete = function() {
<a name="line60"></a>  this.readyState = goog.net.XmlHttp.ReadyState.LOADING;
<a name="line61"></a>  this.onreadystatechange();
<a name="line62"></a>
<a name="line63"></a>  this.readyState = goog.net.XmlHttp.ReadyState.LOADED;
<a name="line64"></a>  this.onreadystatechange();
<a name="line65"></a>
<a name="line66"></a>  this.readyState = goog.net.XmlHttp.ReadyState.INTERACTIVE;
<a name="line67"></a>  this.onreadystatechange();
<a name="line68"></a>
<a name="line69"></a>  this.readyState = goog.net.XmlHttp.ReadyState.COMPLETE;
<a name="line70"></a>  this.onreadystatechange();
<a name="line71"></a>};
<a name="line72"></a>
<a name="line73"></a>
<a name="line74"></a>MockXmlHttp.prototype.open = function(verb, uri, async) {
<a name="line75"></a>};
<a name="line76"></a>
<a name="line77"></a>MockXmlHttp.prototype.abort = function() {};
<a name="line78"></a>
<a name="line79"></a>MockXmlHttp.prototype.setRequestHeader = function(key, value) {
<a name="line80"></a>  this.headers[key] = value;
<a name="line81"></a>};
<a name="line82"></a>
<a name="line83"></a>var lastMockXmlHttp;
<a name="line84"></a>goog.net.XmlHttp.setGlobalFactory(new goog.net.WrapperXmlHttpFactory(
<a name="line85"></a>    function() {
<a name="line86"></a>      lastMockXmlHttp = new MockXmlHttp();
<a name="line87"></a>      return lastMockXmlHttp;
<a name="line88"></a>    },
<a name="line89"></a>    function() {
<a name="line90"></a>      return {};
<a name="line91"></a>    }));
<a name="line92"></a>
<a name="line93"></a>
<a name="line94"></a>var propertyReplacer = new goog.testing.PropertyReplacer();
<a name="line95"></a>var clock;
<a name="line96"></a>var originalEntryPoint =
<a name="line97"></a>    goog.net.XhrIo.prototype.onReadyStateChangeEntryPoint_;
<a name="line98"></a>
<a name="line99"></a>function setUp() {
<a name="line100"></a>  lastMockXmlHttp = null;
<a name="line101"></a>  clock = new goog.testing.MockClock(true);
<a name="line102"></a>}
<a name="line103"></a>
<a name="line104"></a>function tearDown() {
<a name="line105"></a>  propertyReplacer.reset();
<a name="line106"></a>  clock.dispose();
<a name="line107"></a>  goog.net.XhrIo.prototype.onReadyStateChangeEntryPoint_ = originalEntryPoint;
<a name="line108"></a>}
<a name="line109"></a>
<a name="line110"></a>
<a name="line111"></a>function testSyncSend() {
<a name="line112"></a>  MockXmlHttp.syncSend = true;
<a name="line113"></a>  var count = 0;
<a name="line114"></a>
<a name="line115"></a>  var x = new goog.net.XhrIo;
<a name="line116"></a>  goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
<a name="line117"></a>    assertFalse(&#39;Should not fire complete from inside send&#39;, inSend);
<a name="line118"></a>    assertTrue(&#39;Should be succesful&#39;, e.target.isSuccess());
<a name="line119"></a>    count++;
<a name="line120"></a>
<a name="line121"></a>  });
<a name="line122"></a>
<a name="line123"></a>  var inSend = true;
<a name="line124"></a>  x.send(&#39;url&#39;);
<a name="line125"></a>  inSend = false;
<a name="line126"></a>
<a name="line127"></a>  clock.tick(1); // callOnce(f, 0, ...)
<a name="line128"></a>
<a name="line129"></a>  assertEquals(&#39;Complete should have been called once&#39;, 1, count);
<a name="line130"></a>}
<a name="line131"></a>
<a name="line132"></a>function testSyncSendFailure() {
<a name="line133"></a>  MockXmlHttp.syncSend = true;
<a name="line134"></a>  var count = 0;
<a name="line135"></a>
<a name="line136"></a>  var x = new goog.net.XhrIo;
<a name="line137"></a>  goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
<a name="line138"></a>    assertFalse(&#39;Should not fire complete from inside send&#39;, inSend);
<a name="line139"></a>    assertFalse(&#39;Should not be succesful&#39;, e.target.isSuccess());
<a name="line140"></a>    count++;
<a name="line141"></a>  });
<a name="line142"></a>
<a name="line143"></a>  var inSend = true;
<a name="line144"></a>  x.send(&#39;url&#39;);
<a name="line145"></a>  lastMockXmlHttp.status = 404;
<a name="line146"></a>  inSend = false;
<a name="line147"></a>
<a name="line148"></a>  clock.tick(1); // callOnce(f, 0, ...)
<a name="line149"></a>
<a name="line150"></a>  assertEquals(&#39;Complete should have been called once&#39;, 1, count);
<a name="line151"></a>}
<a name="line152"></a>
<a name="line153"></a>
<a name="line154"></a>function testSendRelativeZeroStatus() {
<a name="line155"></a>  MockXmlHttp.syncSend = true;
<a name="line156"></a>  var count = 0;
<a name="line157"></a>
<a name="line158"></a>  var x = new goog.net.XhrIo;
<a name="line159"></a>  goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
<a name="line160"></a>    assertFalse(&#39;Should not fire complete from inside send&#39;, inSend);
<a name="line161"></a>    assertEquals(&#39;Should be the same as &#39;, e.target.isSuccess(),
<a name="line162"></a>        window.location.href.toLowerCase().indexOf(&#39;file:&#39;) == 0);
<a name="line163"></a>    count++;
<a name="line164"></a>  });
<a name="line165"></a>
<a name="line166"></a>  var inSend = true;
<a name="line167"></a>  x.send(&#39;relative&#39;);
<a name="line168"></a>  lastMockXmlHttp.status = 0;
<a name="line169"></a>  inSend = false;
<a name="line170"></a>
<a name="line171"></a>  clock.tick(1); // callOnce(f, 0, ...)
<a name="line172"></a>
<a name="line173"></a>  assertEquals(&#39;Complete should have been called once&#39;, 1, count);
<a name="line174"></a>}
<a name="line175"></a>
<a name="line176"></a>
<a name="line177"></a>function testSendRelativeUriZeroStatus() {
<a name="line178"></a>  MockXmlHttp.syncSend = true;
<a name="line179"></a>  var count = 0;
<a name="line180"></a>
<a name="line181"></a>  var x = new goog.net.XhrIo;
<a name="line182"></a>  goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
<a name="line183"></a>    assertFalse(&#39;Should not fire complete from inside send&#39;, inSend);
<a name="line184"></a>    assertEquals(&#39;Should be the same as &#39;, e.target.isSuccess(),
<a name="line185"></a>        window.location.href.toLowerCase().indexOf(&#39;file:&#39;) == 0);
<a name="line186"></a>    count++;
<a name="line187"></a>  });
<a name="line188"></a>
<a name="line189"></a>  var inSend = true;
<a name="line190"></a>  x.send(goog.Uri.parse(&#39;relative&#39;));
<a name="line191"></a>  lastMockXmlHttp.status = 0;
<a name="line192"></a>  inSend = false;
<a name="line193"></a>
<a name="line194"></a>  clock.tick(1); // callOnce(f, 0, ...)
<a name="line195"></a>
<a name="line196"></a>  assertEquals(&#39;Complete should have been called once&#39;, 1, count);
<a name="line197"></a>}
<a name="line198"></a>
<a name="line199"></a>
<a name="line200"></a>function testSendHttpZeroStatusFailure() {
<a name="line201"></a>  MockXmlHttp.syncSend = true;
<a name="line202"></a>  var count = 0;
<a name="line203"></a>
<a name="line204"></a>  var x = new goog.net.XhrIo;
<a name="line205"></a>  goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
<a name="line206"></a>    assertFalse(&#39;Should not fire complete from inside send&#39;, inSend);
<a name="line207"></a>    assertFalse(&#39;Should not be succesful&#39;, e.target.isSuccess());
<a name="line208"></a>    count++;
<a name="line209"></a>  });
<a name="line210"></a>
<a name="line211"></a>  var inSend = true;
<a name="line212"></a>  x.send(&#39;http://foo&#39;);
<a name="line213"></a>  lastMockXmlHttp.status = 0;
<a name="line214"></a>  inSend = false;
<a name="line215"></a>
<a name="line216"></a>  clock.tick(1); // callOnce(f, 0, ...)
<a name="line217"></a>
<a name="line218"></a>  assertEquals(&#39;Complete should have been called once&#39;, 1, count);
<a name="line219"></a>}
<a name="line220"></a>
<a name="line221"></a>
<a name="line222"></a>function testSendHttpUpperZeroStatusFailure() {
<a name="line223"></a>  MockXmlHttp.syncSend = true;
<a name="line224"></a>  var count = 0;
<a name="line225"></a>
<a name="line226"></a>  var x = new goog.net.XhrIo;
<a name="line227"></a>  goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
<a name="line228"></a>    assertFalse(&#39;Should not fire complete from inside send&#39;, inSend);
<a name="line229"></a>    assertFalse(&#39;Should not be succesful&#39;, e.target.isSuccess());
<a name="line230"></a>    count++;
<a name="line231"></a>  });
<a name="line232"></a>
<a name="line233"></a>  var inSend = true;
<a name="line234"></a>  x.send(&#39;HTTP://foo&#39;);
<a name="line235"></a>  lastMockXmlHttp.status = 0;
<a name="line236"></a>  inSend = false;
<a name="line237"></a>
<a name="line238"></a>  clock.tick(1); // callOnce(f, 0, ...)
<a name="line239"></a>
<a name="line240"></a>  assertEquals(&#39;Complete should have been called once&#39;, 1, count);
<a name="line241"></a>}
<a name="line242"></a>
<a name="line243"></a>
<a name="line244"></a>function testSendHttpUpperUriZeroStatusFailure() {
<a name="line245"></a>  MockXmlHttp.syncSend = true;
<a name="line246"></a>  var count = 0;
<a name="line247"></a>
<a name="line248"></a>  var x = new goog.net.XhrIo;
<a name="line249"></a>  goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
<a name="line250"></a>    assertFalse(&#39;Should not fire complete from inside send&#39;, inSend);
<a name="line251"></a>    assertFalse(&#39;Should not be succesful&#39;, e.target.isSuccess());
<a name="line252"></a>    count++;
<a name="line253"></a>  });
<a name="line254"></a>
<a name="line255"></a>  var inSend = true;
<a name="line256"></a>  x.send(goog.Uri.parse(&#39;HTTP://foo&#39;));
<a name="line257"></a>  lastMockXmlHttp.status = 0;
<a name="line258"></a>  inSend = false;
<a name="line259"></a>
<a name="line260"></a>  clock.tick(1); // callOnce(f, 0, ...)
<a name="line261"></a>
<a name="line262"></a>  assertEquals(&#39;Complete should have been called once&#39;, 1, count);
<a name="line263"></a>}
<a name="line264"></a>
<a name="line265"></a>
<a name="line266"></a>function testSendHttpUriZeroStatusFailure() {
<a name="line267"></a>  MockXmlHttp.syncSend = true;
<a name="line268"></a>  var count = 0;
<a name="line269"></a>
<a name="line270"></a>  var x = new goog.net.XhrIo;
<a name="line271"></a>  goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
<a name="line272"></a>    assertFalse(&#39;Should not fire complete from inside send&#39;, inSend);
<a name="line273"></a>    assertFalse(&#39;Should not be succesful&#39;, e.target.isSuccess());
<a name="line274"></a>    count++;
<a name="line275"></a>  });
<a name="line276"></a>
<a name="line277"></a>  var inSend = true;
<a name="line278"></a>  x.send(goog.Uri.parse(&#39;http://foo&#39;));
<a name="line279"></a>  lastMockXmlHttp.status = 0;
<a name="line280"></a>  inSend = false;
<a name="line281"></a>
<a name="line282"></a>  clock.tick(1); // callOnce(f, 0, ...)
<a name="line283"></a>
<a name="line284"></a>  assertEquals(&#39;Complete should have been called once&#39;, 1, count);
<a name="line285"></a>}
<a name="line286"></a>
<a name="line287"></a>
<a name="line288"></a>function testSendHttpUriZeroStatusFailure() {
<a name="line289"></a>  MockXmlHttp.syncSend = true;
<a name="line290"></a>  var count = 0;
<a name="line291"></a>
<a name="line292"></a>  var x = new goog.net.XhrIo;
<a name="line293"></a>  goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
<a name="line294"></a>    assertFalse(&#39;Should not fire complete from inside send&#39;, inSend);
<a name="line295"></a>    assertFalse(&#39;Should not be succesful&#39;, e.target.isSuccess());
<a name="line296"></a>    count++;
<a name="line297"></a>  });
<a name="line298"></a>
<a name="line299"></a>  var inSend = true;
<a name="line300"></a>  x.send(goog.Uri.parse(&#39;HTTP://foo&#39;));
<a name="line301"></a>  lastMockXmlHttp.status = 0;
<a name="line302"></a>  inSend = false;
<a name="line303"></a>
<a name="line304"></a>  clock.tick(1); // callOnce(f, 0, ...)
<a name="line305"></a>
<a name="line306"></a>  assertEquals(&#39;Complete should have been called once&#39;, 1, count);
<a name="line307"></a>}
<a name="line308"></a>
<a name="line309"></a>
<a name="line310"></a>function testSendHttpsZeroStatusFailure() {
<a name="line311"></a>  MockXmlHttp.syncSend = true;
<a name="line312"></a>  var count = 0;
<a name="line313"></a>
<a name="line314"></a>  var x = new goog.net.XhrIo;
<a name="line315"></a>  goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
<a name="line316"></a>    assertFalse(&#39;Should not fire complete from inside send&#39;, inSend);
<a name="line317"></a>    assertFalse(&#39;Should not be succesful&#39;, e.target.isSuccess());
<a name="line318"></a>    count++;
<a name="line319"></a>  });
<a name="line320"></a>
<a name="line321"></a>  var inSend = true;
<a name="line322"></a>  x.send(&#39;https://foo&#39;);
<a name="line323"></a>  lastMockXmlHttp.status = 0;
<a name="line324"></a>  inSend = false;
<a name="line325"></a>
<a name="line326"></a>  clock.tick(1); // callOnce(f, 0, ...)
<a name="line327"></a>
<a name="line328"></a>  assertEquals(&#39;Complete should have been called once&#39;, 1, count);
<a name="line329"></a>}
<a name="line330"></a>
<a name="line331"></a>
<a name="line332"></a>function testSendFileUpperZeroStatusSuccess() {
<a name="line333"></a>  MockXmlHttp.syncSend = true;
<a name="line334"></a>  var count = 0;
<a name="line335"></a>
<a name="line336"></a>  var x = new goog.net.XhrIo;
<a name="line337"></a>  goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
<a name="line338"></a>    assertFalse(&#39;Should not fire complete from inside send&#39;, inSend);
<a name="line339"></a>    assertTrue(&#39;Should not be succesful&#39;, e.target.isSuccess());
<a name="line340"></a>    count++;
<a name="line341"></a>  });
<a name="line342"></a>
<a name="line343"></a>  var inSend = true;
<a name="line344"></a>  x.send(&#39;FILE:///foo&#39;);
<a name="line345"></a>  lastMockXmlHttp.status = 0;
<a name="line346"></a>  inSend = false;
<a name="line347"></a>
<a name="line348"></a>  clock.tick(1); // callOnce(f, 0, ...)
<a name="line349"></a>
<a name="line350"></a>  assertEquals(&#39;Complete should have been called once&#39;, 1, count);
<a name="line351"></a>}
<a name="line352"></a>
<a name="line353"></a>
<a name="line354"></a>function testSendFileUriZeroStatusSuccess() {
<a name="line355"></a>  MockXmlHttp.syncSend = true;
<a name="line356"></a>  var count = 0;
<a name="line357"></a>
<a name="line358"></a>  var x = new goog.net.XhrIo;
<a name="line359"></a>  goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
<a name="line360"></a>    assertFalse(&#39;Should not fire complete from inside send&#39;, inSend);
<a name="line361"></a>    assertTrue(&#39;Should not be succesful&#39;, e.target.isSuccess());
<a name="line362"></a>    count++;
<a name="line363"></a>  });
<a name="line364"></a>
<a name="line365"></a>  var inSend = true;
<a name="line366"></a>  x.send(goog.Uri.parse(&#39;file:///foo&#39;));
<a name="line367"></a>  lastMockXmlHttp.status = 0;
<a name="line368"></a>  inSend = false;
<a name="line369"></a>
<a name="line370"></a>  clock.tick(1); // callOnce(f, 0, ...)
<a name="line371"></a>
<a name="line372"></a>  assertEquals(&#39;Complete should have been called once&#39;, 1, count);
<a name="line373"></a>}
<a name="line374"></a>
<a name="line375"></a>
<a name="line376"></a>function testSendDummyUriZeroStatusSuccess() {
<a name="line377"></a>  MockXmlHttp.syncSend = true;
<a name="line378"></a>  var count = 0;
<a name="line379"></a>
<a name="line380"></a>  var x = new goog.net.XhrIo;
<a name="line381"></a>  goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
<a name="line382"></a>    assertFalse(&#39;Should not fire complete from inside send&#39;, inSend);
<a name="line383"></a>    assertTrue(&#39;Should not be succesful&#39;, e.target.isSuccess());
<a name="line384"></a>    count++;
<a name="line385"></a>  });
<a name="line386"></a>
<a name="line387"></a>  var inSend = true;
<a name="line388"></a>  x.send(goog.Uri.parse(&#39;dummy:///foo&#39;));
<a name="line389"></a>  lastMockXmlHttp.status = 0;
<a name="line390"></a>  inSend = false;
<a name="line391"></a>
<a name="line392"></a>  clock.tick(1); // callOnce(f, 0, ...)
<a name="line393"></a>
<a name="line394"></a>  assertEquals(&#39;Complete should have been called once&#39;, 1, count);
<a name="line395"></a>}
<a name="line396"></a>
<a name="line397"></a>
<a name="line398"></a>function testSendFileUpperUriZeroStatusSuccess() {
<a name="line399"></a>  MockXmlHttp.syncSend = true;
<a name="line400"></a>  var count = 0;
<a name="line401"></a>
<a name="line402"></a>  var x = new goog.net.XhrIo;
<a name="line403"></a>  goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
<a name="line404"></a>    assertFalse(&#39;Should not fire complete from inside send&#39;, inSend);
<a name="line405"></a>    assertTrue(&#39;Should not be succesful&#39;, e.target.isSuccess());
<a name="line406"></a>    count++;
<a name="line407"></a>  });
<a name="line408"></a>
<a name="line409"></a>  var inSend = true;
<a name="line410"></a>  x.send(goog.Uri.parse(&#39;FILE:///foo&#39;));
<a name="line411"></a>  lastMockXmlHttp.status = 0;
<a name="line412"></a>  inSend = false;
<a name="line413"></a>
<a name="line414"></a>  clock.tick(1); // callOnce(f, 0, ...)
<a name="line415"></a>
<a name="line416"></a>  assertEquals(&#39;Complete should have been called once&#39;, 1, count);
<a name="line417"></a>}
<a name="line418"></a>
<a name="line419"></a>
<a name="line420"></a>function testSendFromListener() {
<a name="line421"></a>  MockXmlHttp.syncSend = true;
<a name="line422"></a>  var count = 0;
<a name="line423"></a>
<a name="line424"></a>  var x = new goog.net.XhrIo;
<a name="line425"></a>  goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
<a name="line426"></a>    count++;
<a name="line427"></a>
<a name="line428"></a>    var e = assertThrows(function() {
<a name="line429"></a>      x.send(&#39;url2&#39;);
<a name="line430"></a>    });
<a name="line431"></a>    assertEquals(&#39;[goog.net.XhrIo] Object is active with another request=url&#39; +
<a name="line432"></a>        &#39;; newUri=url2&#39;, e.message);
<a name="line433"></a>  });
<a name="line434"></a>
<a name="line435"></a>  x.send(&#39;url&#39;);
<a name="line436"></a>
<a name="line437"></a>  clock.tick(1); // callOnce(f, 0, ...)
<a name="line438"></a>
<a name="line439"></a>  assertEquals(&#39;Complete should have been called once&#39;, 1, count);
<a name="line440"></a>}
<a name="line441"></a>
<a name="line442"></a>
<a name="line443"></a>function testStatesDuringEvents() {
<a name="line444"></a>  MockXmlHttp.syncSend = true;
<a name="line445"></a>
<a name="line446"></a>  var x = new goog.net.XhrIo;
<a name="line447"></a>  var readyState = goog.net.XmlHttp.ReadyState.UNINITIALIZED;
<a name="line448"></a>  goog.events.listen(x, goog.net.EventType.READY_STATE_CHANGE, function(e) {
<a name="line449"></a>    readyState++;
<a name="line450"></a>    assertObjectEquals(e.target, x);
<a name="line451"></a>    assertEquals(x.getReadyState(), readyState);
<a name="line452"></a>    assertTrue(x.isActive());
<a name="line453"></a>  });
<a name="line454"></a>  goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
<a name="line455"></a>    assertObjectEquals(e.target, x);
<a name="line456"></a>    assertTrue(x.isActive());
<a name="line457"></a>  });
<a name="line458"></a>  goog.events.listen(x, goog.net.EventType.SUCCESS, function(e) {
<a name="line459"></a>    assertObjectEquals(e.target, x);
<a name="line460"></a>    assertTrue(x.isActive());
<a name="line461"></a>  });
<a name="line462"></a>  goog.events.listen(x, goog.net.EventType.READY, function(e) {
<a name="line463"></a>    assertObjectEquals(e.target, x);
<a name="line464"></a>    assertFalse(x.isActive());
<a name="line465"></a>  });
<a name="line466"></a>
<a name="line467"></a>  x.send(&#39;url&#39;);
<a name="line468"></a>
<a name="line469"></a>  clock.tick(1); // callOnce(f, 0, ...)
<a name="line470"></a>}
<a name="line471"></a>
<a name="line472"></a>
<a name="line473"></a>function testProtectEntryPointCalledOnAsyncSend() {
<a name="line474"></a>  MockXmlHttp.syncSend = false;
<a name="line475"></a>
<a name="line476"></a>  var errorHandlerCallbackCalled = false;
<a name="line477"></a>  var errorHandler = new goog.debug.ErrorHandler(function() {
<a name="line478"></a>    errorHandlerCallbackCalled = true;
<a name="line479"></a>  });
<a name="line480"></a>
<a name="line481"></a>  goog.net.XhrIo.protectEntryPoints(errorHandler);
<a name="line482"></a>
<a name="line483"></a>  var x = new goog.net.XhrIo;
<a name="line484"></a>  goog.events.listen(x, goog.net.EventType.READY_STATE_CHANGE, function(e) {
<a name="line485"></a>    throw Error();
<a name="line486"></a>  });
<a name="line487"></a>
<a name="line488"></a>  x.send(&#39;url&#39;);
<a name="line489"></a>  assertThrows(function() {
<a name="line490"></a>    lastMockXmlHttp.complete();
<a name="line491"></a>  });
<a name="line492"></a>
<a name="line493"></a>  assertTrue(&#39;Error handler callback should be called on async send.&#39;,
<a name="line494"></a>      errorHandlerCallbackCalled);
<a name="line495"></a>}
<a name="line496"></a>
<a name="line497"></a>function testXHRIsDiposedEvenIfAListenerThrowsAnExceptionOnComplete() {
<a name="line498"></a>  MockXmlHttp.syncSend = false;
<a name="line499"></a>
<a name="line500"></a>  var x = new goog.net.XhrIo;
<a name="line501"></a>
<a name="line502"></a>  goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
<a name="line503"></a>    throw Error();
<a name="line504"></a>  }, false, x);
<a name="line505"></a>
<a name="line506"></a>  x.send(&#39;url&#39;);
<a name="line507"></a>  assertThrows(function() {
<a name="line508"></a>    lastMockXmlHttp.complete();
<a name="line509"></a>  });
<a name="line510"></a>
<a name="line511"></a>  // The XHR should have been disposed, even though the listener threw an
<a name="line512"></a>  // exception.
<a name="line513"></a>  assertNull(x.xhr_);
<a name="line514"></a>}
<a name="line515"></a>
<a name="line516"></a>function testDisposeInternalDoesNotAbortXhrRequestObjectWhenActiveIsFalse() {
<a name="line517"></a>  MockXmlHttp.syncSend = false;
<a name="line518"></a>
<a name="line519"></a>  var xmlHttp = goog.net.XmlHttp;
<a name="line520"></a>  var abortCalled = false;
<a name="line521"></a>  var x = new goog.net.XhrIo;
<a name="line522"></a>
<a name="line523"></a>  goog.net.XmlHttp.prototype.abort = function() { abortCalled = true; };
<a name="line524"></a>
<a name="line525"></a>  goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
<a name="line526"></a>    this.active_ = false;
<a name="line527"></a>    this.dispose();
<a name="line528"></a>  }, false, x);
<a name="line529"></a>
<a name="line530"></a>  x.send(&#39;url&#39;);
<a name="line531"></a>  lastMockXmlHttp.complete();
<a name="line532"></a>
<a name="line533"></a>  goog.net.XmlHttp = xmlHttp;
<a name="line534"></a>  assertFalse(abortCalled);
<a name="line535"></a>}
<a name="line536"></a>
<a name="line537"></a>function testCallingAbortFromWithinAbortCallbackDoesntLoop() {
<a name="line538"></a>  var x = new goog.net.XhrIo;
<a name="line539"></a>  goog.events.listen(x, goog.net.EventType.ABORT, function(e) {
<a name="line540"></a>    x.abort(); // Shouldn&#39;t get a stack overflow
<a name="line541"></a>  });
<a name="line542"></a>  x.send(&#39;url&#39;);
<a name="line543"></a>  x.abort();
<a name="line544"></a>}
<a name="line545"></a>
<a name="line546"></a>function testPostSetsContentTypeHeader() {
<a name="line547"></a>  var x = new goog.net.XhrIo;
<a name="line548"></a>
<a name="line549"></a>  x.send(&#39;url&#39;, &#39;POST&#39;, &#39;content&#39;);
<a name="line550"></a>  var headers = lastMockXmlHttp.headers;
<a name="line551"></a>  assertEquals(1, goog.object.getCount(headers));
<a name="line552"></a>  assertEquals(
<a name="line553"></a>      headers[goog.net.XhrIo.CONTENT_TYPE_HEADER],
<a name="line554"></a>      goog.net.XhrIo.FORM_CONTENT_TYPE);
<a name="line555"></a>}
<a name="line556"></a>
<a name="line557"></a>function testNonPostSetsContentTypeHeader() {
<a name="line558"></a>  var x = new goog.net.XhrIo;
<a name="line559"></a>
<a name="line560"></a>  x.send(&#39;url&#39;, &#39;PUT&#39;, &#39;content&#39;);
<a name="line561"></a>  headers = lastMockXmlHttp.headers;
<a name="line562"></a>  assertEquals(1, goog.object.getCount(headers));
<a name="line563"></a>  assertEquals(
<a name="line564"></a>      headers[goog.net.XhrIo.CONTENT_TYPE_HEADER],
<a name="line565"></a>      goog.net.XhrIo.FORM_CONTENT_TYPE);
<a name="line566"></a>}
<a name="line567"></a>
<a name="line568"></a>function testContentTypeIsTreatedCaseInsensitively() {
<a name="line569"></a>  var x = new goog.net.XhrIo;
<a name="line570"></a>
<a name="line571"></a>  x.send(&#39;url&#39;, &#39;POST&#39;, &#39;content&#39;, {&#39;content-type&#39;: &#39;testing&#39;});
<a name="line572"></a>
<a name="line573"></a>  assertObjectEquals(
<a name="line574"></a>      &#39;Headers should not be modified since they already contain a &#39; +
<a name="line575"></a>      &#39;content type definition&#39;,
<a name="line576"></a>      {&#39;content-type&#39;: &#39;testing&#39;},
<a name="line577"></a>      lastMockXmlHttp.headers);
<a name="line578"></a>}
<a name="line579"></a>
<a name="line580"></a>function testIsContentTypeHeader_() {
<a name="line581"></a>  assertTrue(goog.net.XhrIo.isContentTypeHeader_(&#39;content-type&#39;));
<a name="line582"></a>  assertTrue(goog.net.XhrIo.isContentTypeHeader_(&#39;Content-type&#39;));
<a name="line583"></a>  assertTrue(goog.net.XhrIo.isContentTypeHeader_(&#39;CONTENT-TYPE&#39;));
<a name="line584"></a>  assertTrue(goog.net.XhrIo.isContentTypeHeader_(&#39;Content-Type&#39;));
<a name="line585"></a>  assertFalse(goog.net.XhrIo.isContentTypeHeader_(&#39;Content Type&#39;));
<a name="line586"></a>}
<a name="line587"></a>
<a name="line588"></a>function testPostFormDataDoesNotSetContentTypeHeader() {
<a name="line589"></a>  function FakeFormData() {}
<a name="line590"></a>
<a name="line591"></a>  propertyReplacer.set(goog.global, &#39;FormData&#39;, FakeFormData);
<a name="line592"></a>
<a name="line593"></a>  var x = new goog.net.XhrIo;
<a name="line594"></a>  x.send(&#39;url&#39;, &#39;POST&#39;, new FakeFormData());
<a name="line595"></a>  var headers = lastMockXmlHttp.headers;
<a name="line596"></a>  assertTrue(goog.object.isEmpty(headers));
<a name="line597"></a>}
<a name="line598"></a>
<a name="line599"></a>function testNonPostFormDataDoesNotSetContentTypeHeader() {
<a name="line600"></a>  function FakeFormData() {}
<a name="line601"></a>
<a name="line602"></a>  propertyReplacer.set(goog.global, &#39;FormData&#39;, FakeFormData);
<a name="line603"></a>
<a name="line604"></a>  var x = new goog.net.XhrIo;
<a name="line605"></a>  x.send(&#39;url&#39;, &#39;PUT&#39;, new FakeFormData());
<a name="line606"></a>  headers = lastMockXmlHttp.headers;
<a name="line607"></a>  assertTrue(goog.object.isEmpty(headers));
<a name="line608"></a>}
<a name="line609"></a>
<a name="line610"></a>function testFactoryInjection() {
<a name="line611"></a>  var xhr = new MockXmlHttp();
<a name="line612"></a>  var optionsFactoryCalled = 0;
<a name="line613"></a>  var xhrFactoryCalled = 0;
<a name="line614"></a>  var wrapperFactory = new goog.net.WrapperXmlHttpFactory(
<a name="line615"></a>      function() {
<a name="line616"></a>        xhrFactoryCalled++;
<a name="line617"></a>        return xhr;
<a name="line618"></a>      },
<a name="line619"></a>      function() {
<a name="line620"></a>        optionsFactoryCalled++;
<a name="line621"></a>        return {};
<a name="line622"></a>      });
<a name="line623"></a>  var xhrIo = new goog.net.XhrIo(wrapperFactory);
<a name="line624"></a>
<a name="line625"></a>  xhrIo.send(&#39;url&#39;);
<a name="line626"></a>
<a name="line627"></a>  assertEquals(&#39;XHR factory should have been called&#39;, 1, xhrFactoryCalled);
<a name="line628"></a>  assertEquals(&#39;Options factory should have been called&#39;, 1,
<a name="line629"></a>      optionsFactoryCalled);
<a name="line630"></a>}
<a name="line631"></a>
<a name="line632"></a>function testGoogTestingNetXhrIoIsInSync() {
<a name="line633"></a>  var xhrIo = new goog.net.XhrIo();
<a name="line634"></a>  var testingXhrIo = new goog.testing.net.XhrIo();
<a name="line635"></a>
<a name="line636"></a>  var propertyComparator = function(value, key, obj) {
<a name="line637"></a>    if (goog.string.endsWith(key, &#39;_&#39;)) {
<a name="line638"></a>      // Ignore private properties/methods
<a name="line639"></a>      return true;
<a name="line640"></a>    } else if (typeof value == &#39;function&#39; &amp;&amp; typeof this[key] != &#39;function&#39;) {
<a name="line641"></a>      // Only type check is sufficient for functions
<a name="line642"></a>      fail(&#39;Mismatched property:&#39; + key + &#39;: gooo.net.XhrIo has:&lt;&#39; +
<a name="line643"></a>          value + &#39;&gt;; while goog.testing.net.XhrIo has:&lt;&#39; + this[key] + &#39;&gt;&#39;);
<a name="line644"></a>      return true;
<a name="line645"></a>    } else {
<a name="line646"></a>      // Ignore all other type of properties.
<a name="line647"></a>      return true;
<a name="line648"></a>    }
<a name="line649"></a>  };
<a name="line650"></a>
<a name="line651"></a>  goog.object.every(xhrIo, propertyComparator, testingXhrIo);
<a name="line652"></a>}
<a name="line653"></a>
<a name="line654"></a>function testEntryPointRegistry() {
<a name="line655"></a>  var monitor = new goog.debug.EntryPointMonitor();
<a name="line656"></a>  var replacement = function() {};
<a name="line657"></a>  monitor.wrap = goog.testing.recordFunction(
<a name="line658"></a>      goog.functions.constant(replacement));
<a name="line659"></a>
<a name="line660"></a>  goog.debug.entryPointRegistry.monitorAll(monitor);
<a name="line661"></a>  assertTrue(monitor.wrap.getCallCount() &gt;= 1);
<a name="line662"></a>  assertEquals(
<a name="line663"></a>      replacement,
<a name="line664"></a>      goog.net.XhrIo.prototype.onReadyStateChangeEntryPoint_);
<a name="line665"></a>}
<a name="line666"></a>
<a name="line667"></a>function testSetWithCredentials() {
<a name="line668"></a>  // Test on XHR objects that don&#39;t have the withCredentials property (older
<a name="line669"></a>  // browsers).
<a name="line670"></a>  var x = new goog.net.XhrIo;
<a name="line671"></a>  x.setWithCredentials(true);
<a name="line672"></a>  x.send(&#39;url&#39;);
<a name="line673"></a>  assertFalse(
<a name="line674"></a>      &#39;withCredentials should not be set on an XHR object if the property &#39; +
<a name="line675"></a>      &#39;does not exist.&#39;,
<a name="line676"></a>      goog.object.containsKey(lastMockXmlHttp, &#39;withCredentials&#39;));
<a name="line677"></a>
<a name="line678"></a>  // Test on XHR objects that have the withCredentials property.
<a name="line679"></a>  MockXmlHttp.prototype.withCredentials = false;
<a name="line680"></a>  x = new goog.net.XhrIo;
<a name="line681"></a>  x.setWithCredentials(true);
<a name="line682"></a>  x.send(&#39;url&#39;);
<a name="line683"></a>  assertTrue(
<a name="line684"></a>      &#39;withCredentials should be set on an XHR object if the property exists&#39;,
<a name="line685"></a>      goog.object.containsKey(lastMockXmlHttp, &#39;withCredentials&#39;));
<a name="line686"></a>
<a name="line687"></a>  assertTrue(
<a name="line688"></a>      &#39;withCredentials value not set on XHR object&#39;,
<a name="line689"></a>      lastMockXmlHttp.withCredentials);
<a name="line690"></a>
<a name="line691"></a>  // Reset the prototype so it does not effect other tests.
<a name="line692"></a>  delete MockXmlHttp.prototype.withCredentials;
<a name="line693"></a>}
<a name="line694"></a>
<a name="line695"></a>function testGetResponse() {
<a name="line696"></a>  var x = new goog.net.XhrIo;
<a name="line697"></a>
<a name="line698"></a>  // No XHR yet
<a name="line699"></a>  assertEquals(null, x.getResponse());
<a name="line700"></a>
<a name="line701"></a>  // XHR with no .response and no response type, gets text.
<a name="line702"></a>  x.xhr_ = {};
<a name="line703"></a>  x.xhr_.responseText = &#39;text&#39;;
<a name="line704"></a>  assertEquals(&#39;text&#39;, x.getResponse());
<a name="line705"></a>
<a name="line706"></a>  // Response type of text gets text as well.
<a name="line707"></a>  x.setResponseType(goog.net.XhrIo.ResponseType.TEXT);
<a name="line708"></a>  x.xhr_.responseText = &#39;&#39;;
<a name="line709"></a>  assertEquals(&#39;&#39;, x.getResponse());
<a name="line710"></a>
<a name="line711"></a>  // Response type of array buffer gets the array buffer.
<a name="line712"></a>  x.xhr_.mozResponseArrayBuffer = &#39;ab&#39;;
<a name="line713"></a>  x.setResponseType(goog.net.XhrIo.ResponseType.ARRAY_BUFFER);
<a name="line714"></a>  assertEquals(&#39;ab&#39;, x.getResponse());
<a name="line715"></a>
<a name="line716"></a>  // With a response field, it is returned no matter what value it has.
<a name="line717"></a>  x.xhr_.response = undefined;
<a name="line718"></a>  assertEquals(undefined, x.getResponse());
<a name="line719"></a>
<a name="line720"></a>  x.xhr_.response = null;
<a name="line721"></a>  assertEquals(null, x.getResponse());
<a name="line722"></a>
<a name="line723"></a>  x.xhr_.response = &#39;&#39;;
<a name="line724"></a>  assertEquals(&#39;&#39;, x.getResponse());
<a name="line725"></a>
<a name="line726"></a>  x.xhr_.response = &#39;resp&#39;;
<a name="line727"></a>  assertEquals(&#39;resp&#39;, x.getResponse());
<a name="line728"></a>}
<a name="line729"></a>
<a name="line730"></a>function testGetResponseHeaders() {
<a name="line731"></a>  var x = new goog.net.XhrIo();
<a name="line732"></a>
<a name="line733"></a>  // No XHR yet
<a name="line734"></a>  assertEquals(0, goog.object.getCount(x.getResponseHeaders()));
<a name="line735"></a>
<a name="line736"></a>  // Simulate an XHR with 2 headers.
<a name="line737"></a>  var headersRaw = &#39;test1: foo\r\ntest2: bar&#39;;
<a name="line738"></a>
<a name="line739"></a>  propertyReplacer.set(x, &#39;getAllResponseHeaders&#39;,
<a name="line740"></a>                       goog.functions.constant(headersRaw));
<a name="line741"></a>
<a name="line742"></a>  var headers = x.getResponseHeaders();
<a name="line743"></a>  assertEquals(2, goog.object.getCount(headers));
<a name="line744"></a>  assertEquals(&#39;foo&#39;, headers[&#39;test1&#39;]);
<a name="line745"></a>  assertEquals(&#39;bar&#39;, headers[&#39;test2&#39;]);
<a name="line746"></a>}
<a name="line747"></a>
<a name="line748"></a>function testGetResponseHeadersWithColonInValue() {
<a name="line749"></a>  var x = new goog.net.XhrIo();
<a name="line750"></a>
<a name="line751"></a>  // Simulate an XHR with a colon in the http header value.
<a name="line752"></a>  var headersRaw = &#39;test1: f:o:o&#39;;
<a name="line753"></a>
<a name="line754"></a>  propertyReplacer.set(x, &#39;getAllResponseHeaders&#39;,
<a name="line755"></a>                       goog.functions.constant(headersRaw));
<a name="line756"></a>
<a name="line757"></a>  var headers = x.getResponseHeaders();
<a name="line758"></a>  assertEquals(1, goog.object.getCount(headers));
<a name="line759"></a>  assertEquals(&#39;f:o:o&#39;, headers[&#39;test1&#39;]);
<a name="line760"></a>}
<a name="line761"></a>
<a name="line762"></a>function testGetResponseHeadersMultipleValuesForOneKey() {
<a name="line763"></a>  var x = new goog.net.XhrIo();
<a name="line764"></a>
<a name="line765"></a>  // No XHR yet
<a name="line766"></a>  assertEquals(0, goog.object.getCount(x.getResponseHeaders()));
<a name="line767"></a>
<a name="line768"></a>  // Simulate an XHR with 2 headers.
<a name="line769"></a>  var headersRaw = &#39;test1: foo\r\ntest1: bar&#39;;
<a name="line770"></a>
<a name="line771"></a>  propertyReplacer.set(x, &#39;getAllResponseHeaders&#39;,
<a name="line772"></a>                       goog.functions.constant(headersRaw));
<a name="line773"></a>
<a name="line774"></a>  var headers = x.getResponseHeaders();
<a name="line775"></a>  assertEquals(1, goog.object.getCount(headers));
<a name="line776"></a>  assertEquals(&#39;foo, bar&#39;, headers[&#39;test1&#39;]);
<a name="line777"></a>}
<a name="line778"></a>
<a name="line779"></a>function testGetResponseHeadersEmptyHeader() {
<a name="line780"></a>  var x = new goog.net.XhrIo();
<a name="line781"></a>
<a name="line782"></a>  // No XHR yet
<a name="line783"></a>  assertEquals(0, goog.object.getCount(x.getResponseHeaders()));
<a name="line784"></a>
<a name="line785"></a>  // Simulate an XHR with 2 headers, the last of which is empty.
<a name="line786"></a>  var headersRaw = &#39;test2: bar\r\n&#39;;
<a name="line787"></a>
<a name="line788"></a>  propertyReplacer.set(x, &#39;getAllResponseHeaders&#39;,
<a name="line789"></a>                       goog.functions.constant(headersRaw));
<a name="line790"></a>
<a name="line791"></a>  var headers = x.getResponseHeaders();
<a name="line792"></a>  assertEquals(1, goog.object.getCount(headers));
<a name="line793"></a>  assertEquals(&#39;bar&#39;, headers[&#39;test2&#39;]);
<a name="line794"></a>}
</pre>


</body>
</html>
